<script>
export default {
  onLaunch: function () {
    console.log('App Launch')
    this.$store.dispatch('projectSetting/globUpdateInit')
  },
  onShow: function () {
    console.log('App Show')

    // #ifdef MP
    // 小程序更新提醒
    const updateManager = uni.getUpdateManager()
    updateManager.onCheckForUpdate((res) => {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              updateManager.onUpdateReady((_) => {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              })
              updateManager.onUpdateFailed((_) => {
                // 新版本下载失败，提示用户删除后通过冷启动重新打开
                uni.showModal({
                  content: '下载失败，请删除当前小程序后重新打开',
                  showCancel: false,
                })
              })
            }
          },
        })
      }
    })
    // #endif
  },
  onHide: function () {
    console.log('App Hide')
  },
  // 页面不存在监听函数
  onPageNotFound: function (to) {
    // #ifdef H5
    const exchangeList = [
      {
        name: 'course',
        pc: '/course/',
        h5: '/pageCourse/course/details',
      },
      {
        name: 'topic',
        pc: '/topic/details',
        h5: '/pageFound/topic/details',
      },
      {
        name: 'mall',
        pc: '/mall/details',
        h5: '/pageFound/mall/details',
      },
      {
        name: 'user',
        pc: '/user',
        h5: '/pageFound/user/index',
      },
      {
        name: 'share',
        pc: '/exam/share',
        h5: '/pageFound/exam/share',
      },
    ]
    const exchangeData = exchangeList.find((item) => to.path.includes(item.pc))
    if (exchangeData) {
      const list = ['course', 'teacher']
      if (list.includes(exchangeData.name)) {
        let url = to.path.split('/')
        uni.$u.route({
          type: 'redirectTo',
          url: exchangeData.h5,
          params: {
            id: url[url.length - 1],
            ...to.query,
          },
        })
      } else {
        uni.$u.route({
          type: 'redirectTo',
          url: exchangeData.h5,
          params: to.query,
        })
      }
    } else {
      uni.switchTab({
        url: '/pages/index/index',
      })
    }
    // #endif
  },
}
</script>

<style lang="scss">
/* #ifndef APP-PLUS-NVUE */
page {
  background-color: #f7f7f7;
}
/* uView 样式文件 */
@import 'uview-ui/index.scss';
@import './static/font/iconfont.css';
/* #endif */
</style>
